| Class | ActionView::Helpers::InstanceTag |
| In: |
lib/label_with_index.rb
|
| Parent: | Object |
Returns a label tag tailored for labelling an input field for a specified attribute (identified by method) on an object assigned to the template (identified by object). The text of label will default to the attribute name unless you specify it explicitly. Additional options on the label tag can be passed as a hash with options. These options will be tagged onto the HTML as an HTML element attribute as in the example shown.
label(:post, :title) #=> <label for="post_title">Title</label> label(:post, :title, "A short title") #=> <label for="post_title">A short title</label> label(:post, :title, "A short title", :class => "title_label") #=> <label for="post_title" class="title_label">A short title</label> label(:post, :title, "A short title", :class => "title_label", :index => 10) #=> <label for="post_10_title" class="title_label">A short title</label>
# File lib/label_with_index.rb, line 23
23: def to_label_tag(text = nil, options = {})
24: options = options.stringify_keys
25: name_and_id = options.dup
26: add_default_name_and_id(name_and_id)
27: if options.has_key?("value")
28: pretty_tag_value = sanitized_tag_value(options.delete("value"))
29: if options.has_key?("index")
30: options["for"] ||= "#{@object_name}_#{options["index"]}_#{@method_name}_#{pretty_tag_value}"
31: options.delete("index")
32: elsif defined?(@auto_index)
33: options["for"] ||= "#{@object_name}_#{@auto_index}_#{@method_name}_#{pretty_tag_value}"
34: else
35: options["for"] ||= "#{@object_name}_#{@method_name}_#{pretty_tag_value}"
36: end
37: else
38: if options.has_key?("index")
39: options["for"] ||= tag_id_with_index(options["index"])
40: options.delete("index")
41: elsif defined?(@auto_index)
42: options["for"] ||= tag_id_with_index(@auto_index)
43: else
44: options["for"] ||= name_and_id["id"]
45: end
46: end
47: content = (text.blank? ? nil : text.to_s) || method_name.humanize
48: content_tag("label", content, options)
49: end
Returns a radio button tag for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). If the current value of method is tag_value the radio button will be checked. Additional options on the input tag can be passed as a hash with options.
# Let's say that @post.category returns "rails":
radio_button("post", "category", "rails")
radio_button("post", "category", "java")
# => <input type="radio" id="post_category_rails" name="post[category]" value="rails" checked="checked" />
# <input type="radio" id="post_category_java" name="post[category]" value="java" />
radio_button("user", "receive_newsletter", "yes")
radio_button("user", "receive_newsletter", "no")
# => <input type="radio" id="user_receive_newsletter_yes" name="user[receive_newsletter]" value="yes" />
# <input type="radio" id="user_receive_newsletter_no" name="user[receive_newsletter]" value="no" checked="checked" />
# File lib/label_with_index.rb, line 68
68: def to_radio_button_tag(tag_value, options = {})
69: options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
70: options["type"] = "radio"
71: options["value"] = tag_value
72: if options.has_key?("checked")
73: cv = options.delete "checked"
74: checked = cv == true || cv == "checked"
75: else
76: checked = self.class.radio_button_checked?(value(object), tag_value)
77: end
78: options["checked"] = "checked" if checked
79:
80: # It was this:
81: #pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
82: #options["id"] ||= defined?(@auto_index) ?
83: # "#{tag_id_with_index(@auto_index)}_#{pretty_tag_value}" :
84: # "#{tag_id}_#{pretty_tag_value}"
85:
86: # and this plugin changes it to this:
87: pretty_tag_value = sanitized_tag_value(tag_value)
88: if options.has_key?("index")
89: options["id"] ||= "#{@object_name}_#{options["index"]}_#{@method_name}_#{pretty_tag_value}"
90: elsif defined?(@auto_index)
91: options["id"] ||= "#{@object_name}_#{@auto_index}_#{@method_name}_#{pretty_tag_value}"
92: else
93: options["id"] ||= "#{@object_name}_#{@method_name}_#{pretty_tag_value}"
94: end
95:
96: add_default_name_and_id(options)
97: tag("input", options)
98: end